package com.F01.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;

import com.F01.actionForm.F010101ActionForm;
import com.config.Constants;
import com.dao.common.DbOperationExt;
import com.dbtools.Dispose;
import com.exception.SelectException;
import com.info.T_S_YHCZXXinfo;
import com.util.CommonTools;

/**
 * @wangk071226
 * @客户信息管理
 * @数据库操作类
 * @方法命名规则：静态参数名称、例如：查询方法，方法名 QUERYLIST();
 * @数据库某字段如果的新增值，如果要取当前时间，请在数据库中表时间的字段上设置GETDATE(), @ insert语句执行时，会自动插入时间。避免系统时间于服务器时间不统一造成错误。 @ 例表c_customers的customer_date字段。
 */
public class F010101DAO extends DbOperationExt {

	/**
	 * 记录操作日志;
	 */
	Logger log = Logger.getLogger(F010101DAO.class);

	/**
	 * 构造方法获取数据源;
	 * 
	 */
	public F010101DAO() {
		super();
	}

	public F010101DAO(Connection conn) {
		super(conn);
	}

	/**
	 * @查询
	 */
	public List QUERYLIST(F010101ActionForm actionForm,
			HttpServletRequest request) {
		StringBuffer msql = new StringBuffer();
		StringBuffer condition = new StringBuffer();
		/**判断查询条件，拼装查询SQL*/
		/**客户号码*/
		if(actionForm.getCustomers_code()!=null&&!actionForm.getCustomers_code().equals("")){
			condition.append(" and customers_code = '"+actionForm.getCustomers_code().trim()+"' ");
		}
		/**客户名称*/
		if(actionForm.getCustomers_name()!=null&&!actionForm.getCustomers_name().equals("")){
			condition.append(" and customers_name = '"+actionForm.getCustomers_name().trim()+"' ");
		}
		/**电话号码*/
		if(actionForm.getTelephone_number()!=null&&!actionForm.getTelephone_number().equals("")){
			condition.append(" and telephone_number = '"+actionForm.getTelephone_number().trim()+"' ");
		}
		/**传真号码*/
		if(actionForm.getFax_number()!=null&&!actionForm.getFax_number().equals("")){
			condition.append(" and fax_number = '"+actionForm.getFax_number().trim()+"' ");
		}
		/**联 系 人*/
		if(actionForm.getContact_name()!=null&&!actionForm.getContact_name().equals("")){
			condition.append(" and contact_name = '"+actionForm.getContact_name().trim()+"' ");
		}
		/**地    址*/
		if(actionForm.getAddress()!=null&&!actionForm.getAddress().equals("")){
			condition.append(" and address like '%"+actionForm.getAddress().trim()+"%' ");
		}
		List list = new ArrayList();
		Map map = null;
		ResultSet rest = null;
		
		msql.append(" SELECT customers_code,customers_name,address,");
		msql.append(" telephone_number,fax_number,email_adress, ");
		msql.append(" contact_name,remark,customer_date ");
		msql.append(" FROM c_customers  ");
		msql.append(" where 1=1 "+condition.toString()+" ");
		msql.append(" and state='1' ");
		msql.append(" order by  customer_date ");
		try {
			log.info("客户管理 QUERYLIST sql is " + msql.toString());
			rest = super.executeQuery(msql.toString());
			while (rest.next()) { // 循环结果集并在map中添加结果集每行对应字段的值;
				map = new HashMap();
				map.put("customers_code", CommonTools.null2String(rest
						.getString("customers_code")));
				map.put("customers_name", CommonTools.null2String(rest
						.getString("customers_name")));
				map.put("address", CommonTools.null2String(rest
						.getString("address")));
				map.put("telephone_number", CommonTools.null2String(rest
						.getString("telephone_number")));
				map.put("fax_number", CommonTools.null2String(rest
						.getString("fax_number")));
				map.put("email_adress", CommonTools.null2String(rest
						.getString("email_adress")));
				map.put("contact_name", CommonTools.null2String(rest
						.getString("contact_name")));
				map.put("remark", CommonTools.null2String(rest
						.getString("remark")));
				map.put("remarkDisplay", CommonTools.getSubContent(rest
						.getString("remark"), 10));
				map.put("customer_date", CommonTools.getDateTime(rest
						.getDate("customer_date")));
				list.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Dispose.dispose(rest); // 关闭rest及statement;
		}

		return list;
	}

	/**
	 * @新增保存
	 * @要记录操作，在插入后要向操作记录表也插一条语句，也就是用执行多天SQL， @ 所有用super.executeUpdate(sqlList,true)执行
	 */
	public boolean SAVE_INSERT(F010101ActionForm actionForm,
			T_S_YHCZXXinfo t_YhczxxInfo) {
		boolean falg = false;
		List sqlList = new ArrayList();
		/** 存放SQL的LIST */
		StringBuffer yhczsql = new StringBuffer();
		/** 操作记录SQL */
		yhczsql = getInsZzxxSQL(t_YhczxxInfo);
		/** 获得操作记录SQL */
		StringBuffer msql = new StringBuffer();
		msql.append("INSERT INTO c_customers( ");
		msql.append("customers_code, ");
		msql.append("customers_name, ");
		msql.append("address, ");
		msql.append("telephone_number, ");
		msql.append("fax_number, ");
		msql.append("email_adress, ");
		msql.append("contact_name, ");
		msql.append("remark ");
		msql.append(" )VALUES ( ");
		msql.append("'" + CommonTools.getDateTime2() + "', ");
		msql.append("'" + actionForm.getCustomers_name() + "', ");
		msql.append("'" + actionForm.getAddress() + "', ");
		msql.append("'" + actionForm.getTelephone_number() + "', ");
		msql.append("'" + actionForm.getFax_number() + "', ");
		msql.append("'" + actionForm.getEmail_adress() + "', ");
		msql.append("'" + actionForm.getContact_name() + "', ");
		msql.append("'" + actionForm.getRemark() + "')");

		try {
			log.info("客户信息管理 SAVE_INSERT sql is " + msql.toString());
			sqlList.add(msql);
			sqlList.add(yhczsql);
			falg = super.executeUpdate(sqlList, true);
			actionForm.setErrorFlag("0");/**成功*/
		} catch (SQLException ex) {
			if ("1".equals(String.valueOf(ex.getErrorCode()))) {
				/** ex.getErrorCode()返回一，表示主键冲突 */
				actionForm.setErrorFlag("1");/** 主键冲突! */
			} else {
				actionForm.setErrorFlag("1");/**保存失败*/
			}
		} catch (Exception e) {
			actionForm.setErrorFlag("4");/**  */
			e.printStackTrace();
			super.rollback();

		}
		super.commit();

		return falg;
	}

	/**
	 * @编辑
	 */
	public void EDIT(F010101ActionForm actionForm, HttpServletRequest request) {
		ResultSet rest = null;
		StringBuffer msql = new StringBuffer();
		msql.append(" SELECT customers_code,customers_name,address,");
		msql
				.append("telephone_number,fax_number,email_adress,contact_name,remark ");
		msql.append("FROM c_customers  ");
		msql.append(" where customers_code='"
				+ CommonTools.null2String(request
						.getParameter("customers_code").trim()) + "'");
		try {
			log.info("客户管理 QUERYLIST sql is " + msql.toString());
			rest = super.executeQuery(msql.toString());
			while (rest.next()) { // 循环结果集并在map中添加结果集每行对应字段的值;
				actionForm.setCustomers_code(CommonTools.null2String(rest
						.getString("customers_code")));
				actionForm.setCustomers_name(CommonTools.null2String(rest
						.getString("customers_name")));
				actionForm.setAddress(CommonTools.null2String(rest
						.getString("address")));
				actionForm.setTelephone_number(CommonTools.null2String(rest
						.getString("telephone_number")));
				actionForm.setFax_number(CommonTools.null2String(rest
						.getString("fax_number")));
				actionForm.setEmail_adress(CommonTools.null2String(rest
						.getString("email_adress")));
				actionForm.setContact_name(CommonTools.null2String(rest
						.getString("contact_name")));
				actionForm.setRemark((CommonTools.null2String(rest
						.getString("remark"))));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Dispose.dispose(rest); // 关闭rest及statement;
		}
	}

	/**
	 * @编辑保存
	 */
	public boolean SAVE_UPDATE(F010101ActionForm actionForm,
			T_S_YHCZXXinfo t_YhczxxInfo) throws SelectException {
		boolean flag = true;
		List sqlList = new ArrayList();
		StringBuffer yhczsql = new StringBuffer();
		yhczsql = getInsZzxxSQL(t_YhczxxInfo);
		StringBuffer msql = new StringBuffer();
		msql.append(" UPDATE c_customers  SET ");
		msql.append("   customers_name = '" + actionForm.getCustomers_name()
				+ "', ");
		msql.append("   address = '" + actionForm.getAddress() + "', ");
		msql.append("   telephone_number = '"
				+ actionForm.getTelephone_number() + "', ");
		msql.append("   fax_number = '" + actionForm.getFax_number() + "', ");
		msql.append("   email_adress = '" + actionForm.getEmail_adress()
				+ "', ");
		msql.append("   contact_name = '" + actionForm.getContact_name()
				+ "', ");
		msql.append("   remark = '" + actionForm.getRemark() + "' ");
		msql.append(" WHERE  customers_code = '"
				+ actionForm.getCustomers_code() + "' ");
		try {
			log.info("客户信息管理 SAVE_UPDATE sql is " + msql.toString());
			sqlList.add(msql);
			sqlList.add(yhczsql);
			flag = super.executeUpdate(sqlList, true);
			actionForm.setErrorFlag("0");

		} catch (Exception e) {
			actionForm.setErrorFlag("1");
			e.printStackTrace();
			super.rollback();

		}
		super.commit();
		return flag;
	}

	/**
	 * @删除
	 */
	public boolean DELETE(F010101ActionForm actionForm,
			T_S_YHCZXXinfo t_YhczxxInfo, HttpServletRequest request)
			throws SelectException {
		boolean flag = true;
		List sqlList = new ArrayList();
		StringBuffer yhczsql = new StringBuffer();
		yhczsql = getInsZzxxSQL(t_YhczxxInfo);
		StringBuffer msql = new StringBuffer();
		msql.append(" update c_customers set state='0' ");
		msql.append(" WHERE ");
		msql.append(" customers_code = '"
				+ CommonTools.null2String(actionForm.getId().trim()) + "' ");
		try {
			log.info("sql is " + msql.toString());
			sqlList.add(msql);
			sqlList.add(yhczsql);
			flag = super.executeUpdate(sqlList, true);
			actionForm.setErrorFlag("0");
		} catch (Exception e) {
			e.printStackTrace();
			super.rollback();
			actionForm.setErrorFlag("1");
		}
		super.commit();
		return flag;
	}

}
